package recursion;

import java.util.ArrayList;
import java.util.List;

public class Test1 {
     // Definition for a binary tree node.
 public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;

         TreeNode() {
         }

         TreeNode(int val) {
             this.val = val;
         }

         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }
     //递归方式的后序遍历
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer>list=new ArrayList<Integer>();
        PostorderTraversal(root,list);
        return list;

    }
    public void PostorderTraversal(TreeNode root,List<Integer>list){
        if(root==null){
            return;
        }
        PostorderTraversal(root.left,list);
        PostorderTraversal(root.right,list);
        list.add(root.val);
    }




    //递归方式的前序遍历
    List<Integer>list=new ArrayList<>();
    public List<Integer> preorderTraversal1(TreeNode root) {
        if(root==null){
            return list;
        }
        list.add(root.val);
        preorderTraversal1(root.left);
        preorderTraversal1(root.right);
        return list;
    }




}
